/******************** (C) COPYRIGHT 2010 STMicroelectronics ********************
* File Name          : usb_prop.c
* Author             : MCD Application Team
* Version            : V3.2.1
* Date               : 07/05/2010
* Description        : All processing related to Virtual Com Port Demo
********************************************************************************
* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
* CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
*******************************************************************************/

/* Includes ------------------------------------------------------------------*/
#include "usb_lib.h"
#include "usb_conf.h"
#include "usb_prop.h"
#include "usb_desc.h"
#include "usb_pwr.h"
#include "USB_Events.h"
#include "USB_HW_Init.h"
#include "Measurement_HW_Init.h"

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
uint8_t Request = 0;
u32 Max_Lun = 0;
uint8_t *temp_device_desc_return;
uint8_t *temp_config_desc_return;
uint8_t *temp_string_desc_return;

extern uint32_t count_in;
extern DEVICE_INFO *pInformation;
extern DEVICE_INFO	Device_Info;

/* -------------------------------------------------------------------------- */
/*  Structures initializations */
/* -------------------------------------------------------------------------- */

LINE_CODING linecoding =
{
	BAUDRATE, 	/* baud rate*/
	STOPBIT,  	/* stop bits-1*/
	PARITY,   	/* parity - none*/
	BITS    	/* no. of bits 8*/
};


DEVICE Device_Table =
  {
    EP_NUM,
    1
  };


DEVICE_PROP Device_Property =
  {
    USB_init,
    USB_Reset,
    USB_Status_In,
    USB_Status_Out,
    USB_Data_Setup,
    USB_NoData_Setup,
    USB_Get_Interface_Setting,
    USB_GetDeviceDescriptor,
    USB_GetConfigDescriptor,
    USB_GetStringDescriptor,
    0,
    0x40 /*MAX PACKET SIZE*/
  };


USER_STANDARD_REQUESTS User_Standard_Requests =
  {
    USB_GetConfiguration,
    USB_SetConfiguration,
    USB_GetInterface,
    USB_SetInterface,
    USB_GetStatus,
    USB_bulk_ClearFeature,
    USB_SetEndPointFeature,
    USB_SetDeviceFeature,
    USB_SetDeviceAddress
  };


/* Extern variables ----------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Extern function prototypes ------------------------------------------------*/
/* Private functions ---------------------------------------------------------*/

/*******************************************************************************
* Function Name  : USB_init.
* Description    : USB Mouse init routine.
* Input          : None.
* Output         : None.
* Return         : None.
*******************************************************************************/
void USB_init(void)
{

	/* Update the serial number string descriptor with the data from the unique ID*/
	Get_SerialNum();
	
	pInformation->Current_Configuration = 0;
	
	/* Connect the device */
	PowerOn();
	
	/* Perform basic device initialization operations */
	OTG_DEV_Init();
	
	bDeviceState = UNCONNECTED;

}

/*******************************************************************************
* Function Name  : USB_Reset
* Description    : USB Mouse reset routine
* Input          : None.
* Output         : None.
* Return         : None.
*******************************************************************************/
void USB_Reset(void)
{

  	/* Set the device as not configured */
  	pInformation->Current_Configuration = 0;

  	/* Current Feature initialization */
	pInformation->Current_Feature = USB_vcp_ConfigDescriptor[7];
	
	/* Set vcp device with the default Interface*/
	pInformation->Current_Interface = 0;


	#ifdef STM32F10X_CL     
  	/* EP0 is already configured by USB_SIL_Init() function */
  
  	/* Init EP1 IN as Bulk endpoint */
  	OTG_DEV_EP_Init(EP1_IN, OTG_DEV_EP_TYPE_BULK, USB_DATA_SIZE);
  
	/* Init EP2 IN as Interrupt endpoint */
    OTG_DEV_EP_Init(EP2_IN, OTG_DEV_EP_TYPE_INT, USB_INT_SIZE);

	/* Init EP3 OUT as Bulk endpoint */
	OTG_DEV_EP_Init(EP3_OUT, OTG_DEV_EP_TYPE_BULK, USB_DATA_SIZE); 
 
	#else 

  	SetBTABLE(BTABLE_ADDRESS);

	/* Initialize Endpoint 0 */
	SetEPType(ENDP0, EP_CONTROL);
	SetEPTxStatus(ENDP0, EP_TX_STALL);
	SetEPRxAddr(ENDP0, ENDP0_RXADDR);
	SetEPTxAddr(ENDP0, ENDP0_TXADDR);
	Clear_Status_Out(ENDP0);
	SetEPRxCount(ENDP0, Device_Property.MaxPacketSize);
	SetEPRxValid(ENDP0);
	
	/* Initialize Endpoint 1 */
	SetEPType(ENDP1, EP_BULK);
	SetEPTxAddr(ENDP1, ENDP1_TXADDR);
	SetEPTxStatus(ENDP1, EP_TX_NAK);
	SetEPRxStatus(ENDP1, EP_RX_DIS);
	
	/* Initialize Endpoint 2 */
	SetEPType(ENDP2, EP_INTERRUPT);
	SetEPTxAddr(ENDP2, ENDP2_TXADDR);
	SetEPRxStatus(ENDP2, EP_RX_DIS);
	SetEPTxStatus(ENDP2, EP_TX_NAK);
	
	/* Initialize Endpoint 3 */
	SetEPType(ENDP3, EP_BULK);
	SetEPRxAddr(ENDP3, ENDP3_RXADDR);
	SetEPRxCount(ENDP3, USB_BLK_DATA_SIZE);
	SetEPRxStatus(ENDP3, EP_RX_VALID);
	SetEPTxStatus(ENDP3, EP_TX_DIS);
	
	/* Set this device to response on default address */
	SetDeviceAddress(0);
	#endif /* STM32F10X_CL */

	bDeviceState = ATTACHED;

}

/*******************************************************************************
* Function Name  : USB_SetConfiguration.
* Description    : Udpade the device state to configured.
* Input          : None.
* Output         : None.
* Return         : None.
*******************************************************************************/
void USB_SetConfiguration(void)
{

	DEVICE_INFO *pInfo = &Device_Info;
	
	if (pInfo->Current_Configuration != 0)
	{
		/* Device configured */
		bDeviceState = CONFIGURED;
	
	} /* if (pInfo->Current_Configuration != 0) */
	
	count_in=0;

}


/*******************************************************************************
* Function Name  : USB_bulk_ClearFeature
* Description    : Handle the ClearFeature request.
* Input          : None.
* Output         : None.
* Return         : None.
*******************************************************************************/
void USB_bulk_ClearFeature(void)
{

}


/*******************************************************************************
* Function Name  : USB_SetDeviceAddress.
* Description    : Udpade the device state to addressed.
* Input          : None.
* Output         : None.
* Return         : None.
*******************************************************************************/
void USB_SetDeviceAddress (void)
{
  bDeviceState = ADDRESSED;
}

/*******************************************************************************
* Function Name  : USB_Status_In.
* Description    : USB Status In Routine.
* Input          : None.
* Output         : None.
* Return         : None.
*******************************************************************************/
void USB_Status_In(void)
{

}

/*******************************************************************************
* Function Name  : USB_Status_Out
* Description    : USB Status OUT Routine.
* Input          : None.
* Output         : None.
* Return         : None.
*******************************************************************************/
void USB_Status_Out(void)
{

}

/*******************************************************************************
* Function Name  : USB_Data_Setup
* Description    : handle the data class specific requests
* Input          : Request Nb.
* Output         : None.
* Return         : USB_UNSUPPORT or USB_SUCCESS.
*******************************************************************************/
RESULT USB_Data_Setup(uint8_t RequestNo)
{
	uint8_t    *(*CopyRoutine)(uint16_t);
	
	CopyRoutine = NULL;
	
	if (RequestNo == GET_LINE_CODING)
	{
		if (Type_Recipient == (CLASS_REQUEST | INTERFACE_RECIPIENT))
		{
	  		CopyRoutine = USB_vcp_GetLineCoding;
		}
	}

	else if (RequestNo == SET_LINE_CODING)
	{
		if (Type_Recipient == (CLASS_REQUEST | INTERFACE_RECIPIENT))
		{
	  		CopyRoutine = USB_vcp_SetLineCoding;
		}
		Request = SET_LINE_CODING;
	}

	if (CopyRoutine == NULL)
	{
		return USB_UNSUPPORT;
	}
	
	pInformation->Ctrl_Info.CopyData = CopyRoutine;
	pInformation->Ctrl_Info.Usb_wOffset = 0;
	(*CopyRoutine)(0);
	
	return USB_SUCCESS;
}

/*******************************************************************************
* Function Name  : USB_NoData_Setup.
* Description    : handle the no data class specific requests.
* Input          : Request Nb.
* Output         : None.
* Return         : USB_UNSUPPORT or USB_SUCCESS.
*******************************************************************************/
RESULT USB_NoData_Setup(uint8_t RequestNo)
{

	if (Type_Recipient == (CLASS_REQUEST | INTERFACE_RECIPIENT))
	{
		if (RequestNo == SET_COMM_FEATURE)
		{
			return USB_SUCCESS;
		}
		else if (RequestNo == SET_CONTROL_LINE_STATE)
		{
			return USB_SUCCESS;
		}
	}

	return USB_UNSUPPORT;

}

/*******************************************************************************
* Function Name  : USB_GetDeviceDescriptor.
* Description    : Gets the device descriptor.
* Input          : Length.
* Output         : None.
* Return         : The address of the device descriptor.
*******************************************************************************/
uint8_t *USB_GetDeviceDescriptor(uint16_t Length)
{

	ONE_DESCRIPTOR Device_Descriptor =
	{
	  (uint8_t*)USB_vcp_DeviceDescriptor,
	  USB_SIZ_DEVICE_DESC
	};

	temp_device_desc_return = Standard_GetDescriptorData(Length, &Device_Descriptor);
	
	return temp_device_desc_return;

}

/*******************************************************************************
* Function Name  : USB_GetConfigDescriptor.
* Description    : get the configuration descriptor.
* Input          : Length.
* Output         : None.
* Return         : The address of the configuration descriptor.
*******************************************************************************/
uint8_t *USB_GetConfigDescriptor(uint16_t Length)
{

	ONE_DESCRIPTOR Config_Descriptor =
    {
      (uint8_t*)USB_vcp_ConfigDescriptor,
      USB_VCP_SIZ_CONFIG_DESC
    };
	temp_config_desc_return = Standard_GetDescriptorData(Length, &Config_Descriptor);

	return temp_config_desc_return;

}

/*******************************************************************************
* Function Name  : USB_GetStringDescriptor
* Description    : Gets the string descriptors according to the needed index
* Input          : Length.
* Output         : None.
* Return         : The address of the string descriptors.
*******************************************************************************/
uint8_t *USB_GetStringDescriptor(uint16_t Length)
{
  uint8_t wValue0 = pInformation->USBwValue0;

  if (wValue0 > 4)
  {
    return NULL;
  }
  else
  {

	  ONE_DESCRIPTOR String_Descriptor[4] =
	  {
	      {(uint8_t*)USB_StringLangID, USB_SIZ_STRING_LANGID},
	      {(uint8_t*)USB_StringVendor, USB_SIZ_STRING_VENDOR},
	      {(uint8_t*)USB_vcp_StringProduct, USB_SIZ_STRING_PRODUCT},
	      {(uint8_t*)USB_StringSerial, USB_SIZ_STRING_SERIAL}
	  };

	  temp_string_desc_return = Standard_GetDescriptorData(Length, &String_Descriptor[wValue0]);

    return temp_string_desc_return;
  }

}

/*******************************************************************************
* Function Name  : USB_Get_Interface_Setting.
* Description    : test the interface and the alternate setting according to the
*                  supported one.
* Input1         : uint8_t: Interface : interface number.
* Input2         : uint8_t: AlternateSetting : Alternate Setting number.
* Output         : None.
* Return         : The address of the string descriptors.
*******************************************************************************/
RESULT USB_Get_Interface_Setting(uint8_t Interface, uint8_t AlternateSetting)
{

  if (AlternateSetting > 0)
  {
	  return USB_UNSUPPORT;
  }
  else if (Interface > 1)
  {
    return USB_UNSUPPORT;
  }

  return USB_SUCCESS;

}

/*******************************************************************************
* Function Name  : USB_vcp_GetLineCoding.
* Description    : send the linecoding structure to the PC host.
* Input          : Length.
* Output         : None.
* Return         : Inecoding structure base address.
*******************************************************************************/
uint8_t *USB_vcp_GetLineCoding(uint16_t Length)
{
  if (Length == 0)
  {
    pInformation->Ctrl_Info.Usb_wLength = sizeof(linecoding);
    return NULL;
  }
  return(uint8_t *)&linecoding;
}

/*******************************************************************************
* Function Name  : USB_vcp_SetLineCoding.
* Description    : Set the linecoding structure fields.
* Input          : Length.
* Output         : None.
* Return         : Linecoding structure base address.
*******************************************************************************/
uint8_t *USB_vcp_SetLineCoding(uint16_t Length)
{
  if (Length == 0)
  {
    pInformation->Ctrl_Info.Usb_wLength = sizeof(linecoding);
    return NULL;
  }
  return(uint8_t *)&linecoding;
}

/*******************************************************************************
* Function Name  : Get_Max_Lun
* Description    : Handle the Get Max Lun request.
* Input          : uint16_t Length.
* Output         : None.
* Return         : None.
*******************************************************************************/
uint8_t *Get_Max_Lun(uint16_t Length)
{
  if (Length == 0)
  {
    pInformation->Ctrl_Info.Usb_wLength = LUN_DATA_LENGTH;
    return 0;
  }
  else
  {
    return((uint8_t*)(&Max_Lun));
  }
}

/******************* (C) COPYRIGHT 2010 STMicroelectronics *****END OF FILE****/

